package fr.castorflex.SoundWidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
import android.widget.RemoteViews;

public class SoundWidget extends AppWidgetProvider {

	public static String CLICK_ON_ALARM = "Click on Alarm";
	public static String CLICK_ON_MEDIA = "Click on Media";
	public static String CLICK_ON_ALERTS = "Click on Alerts";
	public static String CLICK_ON_RINGER = "Click on Ringer";
	public static String CLICK_ON_SYSTEM = "Click on System";
	public static String CLICK_ON_VOICE = "Click on Voice";
	public static String CLICK_ON_VIBRATOR = "Click on Vibrator1";

	// false if not muted, true if muted
	private static boolean alarmSoundMuted;
	private static boolean mediaSoundMuted;
	private static boolean alertsSoundMuted;
	private static boolean ringerSoundMuted;
	private static boolean systemSoundMuted;
	private static boolean voiceSoundMuted;
	private static boolean vibratorSoundMuted;

	private static int alarmSoundVolume = 0;
	private static int mediaSoundVolume = 0;
	private static int alertsSoundVolume = 0;
	private static int ringerSoundVolume = 0;
	private static int systemSoundVolume = 0;
	private static int voiceSoundVolume = 0;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		Log.i(this.getClass().getName(), "onupdate started");

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.main);

		Intent active = new Intent(context, SoundWidget.class);
		active.setAction(CLICK_ON_ALARM);
		PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
				0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewAlarm,
				actionPendingIntent);

		active = new Intent(context, SoundWidget.class);
		active.setAction(CLICK_ON_MEDIA);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewMedia,
				actionPendingIntent);

		active = new Intent(context, SoundWidget.class);
		active.setAction(CLICK_ON_ALERTS);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewAlerts,
				actionPendingIntent);

		active = new Intent(context, SoundWidget.class);
		active.setAction(CLICK_ON_RINGER);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewRinger,
				actionPendingIntent);

		active = new Intent(context, SoundWidget.class);
		active.setAction(CLICK_ON_SYSTEM);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewSystem,
				actionPendingIntent);

		active = new Intent(context, SoundWidget.class);
		active.setAction(CLICK_ON_VOICE);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewVoice,
				actionPendingIntent);

		active = new Intent(context, SoundWidget.class);
		active.setAction(CLICK_ON_VIBRATOR);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewVibrator,
				actionPendingIntent);

		init(context);
		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
		super.onUpdate(context, appWidgetManager, appWidgetIds);

	}

	public int recupMode(AudioManager audio) {
		return audio.getRingerMode();
	}

	public boolean isSilentMode(AudioManager audio) {
		return audio.getRingerMode() == AudioManager.RINGER_MODE_SILENT;
	}

	public boolean isVibrateMode(AudioManager audio) {
		return audio.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE;
	}

	public boolean isNormalMode(AudioManager audio) {
		return audio.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
	}

	public void init(Context context) {
		AudioManager audio = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);

		alarmSoundMuted = audio.getStreamVolume(AudioManager.STREAM_ALARM) == 0 ? true
				: false;
		mediaSoundMuted = audio.getStreamVolume(AudioManager.STREAM_MUSIC) == 0 ? true
				: false;
		voiceSoundMuted = audio
				.getStreamVolume(AudioManager.STREAM_VOICE_CALL) == 0 ? true
				: false;

		if (isSilentMode(audio) || isVibrateMode(audio)) {
			ringerSoundMuted = true;
			systemSoundMuted = true;
			alertsSoundMuted = true;
		} else {
			ringerSoundMuted = audio.getStreamVolume(AudioManager.STREAM_RING) == 0 ? true
					: false;
			systemSoundMuted = audio
					.getStreamVolume(AudioManager.STREAM_SYSTEM) == 0 ? true
					: false;
			alertsSoundMuted = audio
					.getStreamVolume(AudioManager.STREAM_NOTIFICATION) == 0 ? true
					: false;
		}

		if (alarmSoundVolume == 0)
			if (alarmSoundMuted)
				alarmSoundVolume = audio
						.getStreamMaxVolume(AudioManager.STREAM_ALARM);
			else
				alarmSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_ALARM);

		if (mediaSoundVolume == 0)
			if (mediaSoundMuted)
				mediaSoundVolume = audio
						.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
			else
				mediaSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_MUSIC);
		
		if (alertsSoundVolume == 0)
			if (alertsSoundMuted)
				alertsSoundVolume = audio
						.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
			else
				alertsSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_NOTIFICATION);

		if (ringerSoundVolume == 0)
			if (ringerSoundMuted)
				ringerSoundVolume = audio
						.getStreamMaxVolume(AudioManager.STREAM_RING);
			else
				ringerSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_RING);

		if (systemSoundVolume == 0)
			if (systemSoundMuted)
				systemSoundVolume = audio
						.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
			else
				systemSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_SYSTEM);

		if (voiceSoundVolume == 0)
			if (voiceSoundMuted)
				voiceSoundVolume = audio
						.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
			else
				voiceSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
		
		
		if (audio.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION) != AudioManager.VIBRATE_SETTING_OFF
				|| audio.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) != AudioManager.VIBRATE_SETTING_OFF) {
			Log.i(context.getClass().toString(), "mode flex1");
			vibratorSoundMuted = false;
		} else{
			vibratorSoundMuted = true;
			Log.i(context.getClass().toString(), "mode flex2");
		}

		reloadAll(context, audio);

	}

	@Override
	public void onReceive(Context context, Intent intent) {

		final String action = intent.getAction();

		AudioManager audio = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);

		if (action.equals(CLICK_ON_ALARM)) {
			if (alarmSoundMuted)
				audio.setStreamVolume(AudioManager.STREAM_ALARM,
						alarmSoundVolume, 0);
			else {
				audio.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);

			}
			alarmSoundMuted = !alarmSoundMuted;

		} else if (action.equals(CLICK_ON_MEDIA)) {

			if (mediaSoundMuted)
				audio.setStreamVolume(AudioManager.STREAM_MUSIC,
						mediaSoundVolume, 0);
			else {
				mediaSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_MUSIC);
				audio.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
			}
			mediaSoundMuted = !mediaSoundMuted;

		} else if (action.equals(CLICK_ON_RINGER)) {
			if (ringerSoundMuted)
				audio.setStreamVolume(AudioManager.STREAM_RING,
						ringerSoundVolume, 0);
			else {
				ringerSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_RING);
				audio.setStreamVolume(AudioManager.STREAM_RING, 0, 0);

			}
			ringerSoundMuted = !ringerSoundMuted;

		} else if (action.equals(CLICK_ON_ALERTS)) {
			if (alertsSoundMuted)
				audio.setStreamVolume(AudioManager.STREAM_NOTIFICATION,
						alertsSoundVolume, 0);
			else {
				alertsSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
				audio.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0);

			}
			alertsSoundMuted = !alertsSoundMuted;

		} else if (action.equals(CLICK_ON_SYSTEM)) {
			if (systemSoundMuted)
				audio.setStreamVolume(AudioManager.STREAM_SYSTEM,
						systemSoundVolume, 0);
			else {
				systemSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_SYSTEM);
				audio.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, 0);

			}
			systemSoundMuted = !systemSoundMuted;

		} else if (action.equals(CLICK_ON_VOICE)) {
			if (voiceSoundMuted)
				audio.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
						voiceSoundVolume, 0);
			else {
				voiceSoundVolume = audio
						.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
				audio.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 0, 0);

			}
			voiceSoundMuted = !voiceSoundMuted;

		} else if (action.equals(CLICK_ON_VIBRATOR)) {
			vibratorSoundMuted = !vibratorSoundMuted;

		} else {
			super.onReceive(context, intent);
			return;
		}
		reloadAll(context, audio);

		super.onReceive(context, intent);
	}

	public void reloadAll(Context context, AudioManager audio) {
		if (vibratorSoundMuted) {
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
					AudioManager.VIBRATE_SETTING_OFF);
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
					AudioManager.VIBRATE_SETTING_OFF);
		} else {
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
					AudioManager.VIBRATE_SETTING_ON);
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
					AudioManager.VIBRATE_SETTING_ON);
		}

		if (ringerSoundMuted && alertsSoundMuted && systemSoundMuted) {
			if (vibratorSoundMuted)
				audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			else {
				audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

			}
		} else {
			audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

		}

		updateImages(context);

	}

	public void updateImages(Context context) {

		Log.i(this.getClass().toString(), "updateImages");

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.main);

		if (alarmSoundMuted) {
			remoteViews.setImageViewResource(R.id.imageViewAlarm,
					R.xml.alarm_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewAlarm,
					R.xml.alarm_selector_on);
		}
		if (mediaSoundMuted) {
			remoteViews.setImageViewResource(R.id.imageViewMedia,
					R.xml.media_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewMedia,
					R.xml.media_selector_on);
		}
		if (alertsSoundMuted) {
			remoteViews.setImageViewResource(R.id.imageViewAlerts,
					R.xml.alerts_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewAlerts,
					R.xml.alerts_selector_on);
		}
		if (ringerSoundMuted) {
			remoteViews.setImageViewResource(R.id.imageViewRinger,
					R.xml.ringer_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewRinger,
					R.xml.ringer_selector_on);
		}

		if (systemSoundMuted) {
			remoteViews.setImageViewResource(R.id.imageViewSystem,
					R.xml.system_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewSystem,
					R.xml.system_selector_on);
		}

		if (voiceSoundMuted) {
			remoteViews.setImageViewResource(R.id.imageViewVoice,
					R.xml.voice_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewVoice,
					R.xml.voice_selector_on);
		}

		if (vibratorSoundMuted) {
			remoteViews.setImageViewResource(R.id.imageViewVibrator,
					R.xml.vibrator_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewVibrator,
					R.xml.vibrator_selector_on);
		}

		AppWidgetManager manager = AppWidgetManager.getInstance(context);
		ComponentName myWidget = new ComponentName(context, SoundWidget.class);
		manager.updateAppWidget(myWidget, remoteViews);
	}

}
